package com.leetcode;

/**
 * TODO
 *
 * @author lefly
 * @version 1.0
 * @classname No121
 * @description 121. 买卖股票的最佳时机
 * @date 2021/3/9 下午7:17
 */
public class No121 {

    public static void main(String[] args) {
        int[] arr = {7,1,5,3,6,4};
        int maxProfit = maxProfit(arr);
        System.out.println(maxProfit);
    }

    public static int maxProfit(int[] prices) {
        if(prices == null || prices.length == 0){
            return 0;
        }
        int len = prices.length;
        int minSoFar = prices[0];
        int maxProfit = 0;
        for (int i = 1; i < len; i++) {
            if(minSoFar > prices[i]){
                minSoFar = prices[i];
            }else {
                maxProfit = Math.max(maxProfit,prices[i] - minSoFar);
            }
        }
        return maxProfit;

    }

}
